#include "widget.h"
#include "ui_widget.h"
#include<fstream>
#include<iostream>
#include<string>
#include"QDebug"
#include"QListWidget"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    std::ifstream file("C:\\Users\\x\\Desktop\\book.txt");//从文件读取
    std::string line;

   while(getline(file,line))
   {
       ui->listWidget->addItem(QString::fromStdString(line));
   }

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    QString content=ui->lineEdit->text();
    if(content.isEmpty())
    {
        return;
    }
    ui->listWidget->addItem(content);
}

void Widget::on_pushButton_2_clicked()
{
    int row=ui->listWidget->currentRow();
    ui->listWidget->takeItem(row);
}

void Widget::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)//现在选中的和之前选中的
{
    if(current!=nullptr)
    {
        qDebug()<<current->text();
      }
    if(previous!=nullptr)
    {
        qDebug()<<previous->text();
      }
}
